package cn.lcsjsxy.yxz.day.Thread;

import java.util.Hashtable;
import java.util.logging.Handler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//同步
public class synchronize {
    public static void main(String[] args)  throws Exception{

        //synchronized 同步关键字
        //多个线程访问同步方法时,只能一个一个访问,同步操作
//        new Hashtable<>();
//        synchronized关键字还可以修饰代码,称之为同步代码块
        /*
        synchronized(同步对象){
            处理逻辑
        }
        */
        Num num = new Num();

        User user = new User(num);
        user.start();

        Bank bank = new Bank(num);
        bank.start();

    }

}
class Num{

}
class Bank extends Thread{
    public Num num;
    public Bank( Num num){
        this.num = num;
    }
    public void run(){
        synchronized (num){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("开始营业!");
            num.notifyAll();
        }
    }
}
class User extends Thread{
//    public synchronized void test(){
//
//    }
    public Num num;
    public User( Num num){
        this.num = num;
    }
    public void run(){

        synchronized ( num){
            System.out.println("我第一,但还没开始营业");
            try {
                num.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println("叫到我了,跑路了");

        }

    }

}